package com.zl.tesseract.scanner.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zl.tesseract.R;
import com.zl.tesseract.scanner.beans.MoveBean;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;

public class MoveResultListAdapter extends RecyclerView.Adapter<MoveResultListAdapter.ViewHolder> {

    private Context mContext;

    private List<MoveBean> moveBeanList;

    public MoveResultListAdapter(Context context, List<MoveBean> list) {
        this.mContext = context;
        this.moveBeanList = list;
    }

    public void setList(List<MoveBean> list) {
        moveBeanList.clear();
        append(list);
    }

    public void append(List<MoveBean> list) {
        int positionStart = moveBeanList.size();
        int itemCount = list.size();
        moveBeanList.addAll(list);
        if (positionStart > 0 && itemCount > 0) {
            notifyItemRangeInserted(positionStart, itemCount);
        } else {
            notifyDataSetChanged();
        }
    }

    public void append(MoveBean item) {
        int positionStart = moveBeanList.size();
        moveBeanList.add(item);
        if (positionStart > 0) {
            notifyItemRangeInserted(positionStart, 1);
        } else {
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_move_result_list, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        final EditText edt_order = holder.edt_order;
        final ImageView iv_edit = holder.iv_edit;
        TextView tv_shelve = holder.tv_shelve;
        Button delete = holder.delete;

        final MoveBean ipIntervalBean = moveBeanList.get(position);

        edt_order.setText(ipIntervalBean.getOrder_id() + "");
//        tv_shelve.setText(ipIntervalBean.getShelve_name() + "货架");
        iv_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt_order.setEnabled(true);
            }
        });

        // 侧滑删除功能
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SweetAlertDialog(mContext, SweetAlertDialog.WARNING_TYPE)
                        .setTitleText("是否确认删除!")
                        .setConfirmText("删除")
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sDialog) {
                                sDialog.dismissWithAnimation();
                                moveBeanList.remove(position);
                                notifyDataSetChanged();
                            }
                        })
                        .show();

            }
        });
    }

    @Override
    public int getItemCount() {
        return moveBeanList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        EditText edt_order;
        ImageView iv_edit;
        TextView tv_shelve;
        Button delete;

        ViewHolder(View itemView) {
            super(itemView);
            edt_order = itemView.findViewById(R.id.edt_order);
            iv_edit = itemView.findViewById(R.id.iv_edit);
            tv_shelve = itemView.findViewById(R.id.tv_shelve);
            delete = itemView.findViewById(R.id.delete);
        }
    }

}
